package com.wan37.logic.utils;

import com.wan37.logic.shop.domain.vo.CommodityConfigVO;
import com.wan37.protobuf.constant.CommonBase;
import com.wan37.logic.domain.vo.*;
import com.wan37.logic.handler.LogicServerHandler;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.*;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author : luoyong
 * @date : 2020-05-27 17:21
 **/
@Component
public final class RouteUtils {

    @Autowired
    private RoleServiceCache roleServiceCache;

    private RouteUtils() {
    }

    /**
     * 向客户端回复消息.
     *
     * @param code
     * @param content
     * @param netId
     * @param userId
     */
    public void sendResponse(int code, String content, long netId, String userId) {
        Game.SResponse.Builder sb = Game.SResponse.newBuilder();
        sb.setCode(code);
        sb.setContent(content);
        Game.SResponse sResponse = sb.build();
        //sResponse消息
        ByteBuf sendBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                netId, userId, PtoNum.GAMESRESPONSE, sResponse);

        //通过gateServer转发给客户端.
        LogicServerHandler.getGateConnCtx().writeAndFlush(sendBuf);
    }


    public void sendGameAoiResponse(int code, String content, List<AllRoleVO> allRoleVOList, long netId, String userId) {
        Game.SGameAoiResponse.Builder sb = Game.SGameAoiResponse.newBuilder();
        sb.setCode(code);
        sb.setContent(content);
        GameEntity.AllRole.Builder ab = null;
        if (!allRoleVOList.isEmpty()) {
            for (AllRoleVO allRoleVO : allRoleVOList) {
                ab = GameEntity.AllRole.newBuilder();
                allRoleVOToAllRole(ab, allRoleVO);
                sb.addAllRole(ab.build());
            }
        }
        Game.SGameAoiResponse sGameAoiResponse = sb.build();
        ByteBuf setBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                netId, userId, PtoNum.SGAMEAOIRESPONSE, sGameAoiResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(setBuf);
    }

    /**
     * 攻击完成后广播对于实体消息
     *
     * @param allRoleVOList 状态改变的实体
     * @param roleId
     * @param skillId
     * @param userIds       接受广播的用户
     */
    public void sendGameAttackBroadcast(List<AllRoleVO> allRoleVOList, String roleId, String skillId, Set<String> userIds) {
        Game.SGameAttackBroadcast.Builder sb = Game.SGameAttackBroadcast.newBuilder();

        if (!allRoleVOList.isEmpty()) {
            for (AllRoleVO allRoleVO : allRoleVOList) {
                GameEntity.AllRole.Builder ab = GameEntity.AllRole.newBuilder();
                allRoleVOToAllRole(ab, allRoleVO);
                sb.addAllRole(ab.build());
            }
        }
        if (!userIds.isEmpty()) {
            GameEntity.stringMsg.Builder smb = GameEntity.stringMsg.newBuilder();
            for (String s : userIds) {
                sb.addUserId(smb.setStringId(s).build());
            }
        }
        sb.setSenderId(roleId);
        sb.setSkillId(skillId);
        Game.SGameAttackBroadcast sGameAttackBroadcast = sb.build();
        ByteBuf setBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                0, "a", PtoNum.SGAMEATTACKBROADCAST, sGameAttackBroadcast);
        LogicServerHandler.getGateConnCtx().writeAndFlush(setBuf);
    }

    /**
     * 返回技能释放处理后的消息体
     *
     * @param code
     * @param userIds
     * @param allRoleId          释放技能的实体
     * @param allRoleIdList      受到此技能影响的实体
     * @param skillId
     * @param allRoleVONumberMap 对应实体，对应属性，改变后的数值
     */
    public void sendAttackBroadcastResponse(int code, Set<String> userIds, String allRoleId,
                                            List<String> allRoleIdList, String skillId, Map<String, Map<String, Long>> allRoleVONumberMap) {
        GameFighting.AttackBroadcastResponse.Builder avrb = GameFighting.AttackBroadcastResponse.newBuilder();
        avrb.setCode(code);
        if (!userIds.isEmpty()) {
            GameEntity.stringMsg.Builder smb = GameEntity.stringMsg.newBuilder();
            for (String s : userIds) {
                avrb.addReceiverId(smb.setStringId(s).build());
            }
        }
        GameFighting.AttackResponse.Builder arb = GameFighting.AttackResponse.newBuilder();
        arb.setCode(code);
        arb.setAllRoleId(allRoleId);
        if (allRoleIdList != null && !allRoleIdList.isEmpty()) {
            GameEntity.stringMsg.Builder smb = GameEntity.stringMsg.newBuilder();
            for (String s : allRoleIdList) {
                arb.addAllRoleId2(smb.setStringId(s).build());
            }
        }
        arb.setSkillId(skillId);
        if (allRoleVONumberMap != null) {
            allRoleVONumberMap.forEach((key, numberMap) -> {
                GameFighting.AttackResponse.AttackResult.Builder ar = GameFighting.AttackResponse.AttackResult.newBuilder();
                ar.setAllRoleId(key);
                numberMap.forEach((s, l) -> {
                    GameFighting.AttackResponse.AttackResult.NumberMap.Builder nmb = GameFighting.AttackResponse.AttackResult.NumberMap.newBuilder();
                    nmb.setCommonBase(s);
                    nmb.setResult(l);
                    ar.addNumberMap(nmb.build());
                });
                arb.addResult(ar.build());
            });
        }
        avrb.setAttackResponse(arb.build());
        GameFighting.AttackBroadcastResponse attackBroadcast = avrb.build();

        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                "", PtoNum.ATTACKBROADCASTRESPONSE, attackBroadcast);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 发送广播技能消息
     *
     * @param code
     * @param content
     * @param allRoleIdToSkillVOMap
     * @param receiverIdSet
     */
    public void sendSkillResponseBroadcast(int code, String content, Map<String, Map<String, SkillVO>> allRoleIdToSkillVOMap,
                                           Set<String> receiverIdSet) {
        Game.SkillResponseBroadcast.Builder srbb = Game.SkillResponseBroadcast.newBuilder();
        for (String receiverId : receiverIdSet) {
            srbb.addReceiverId(GameEntity.stringMsg.newBuilder().setStringId(receiverId).build());
        }
        Game.SGameSkillResponse.Builder ssr = Game.SGameSkillResponse.newBuilder();
        ssr.setCode(code);
        ssr.setContent(content);
        allRoleIdToSkillVOMap.forEach((allRoleId, skillVOMap) -> {
            if (skillVOMap != null) {
                Game.AllRoleIdToSkillList.Builder aritkl = Game.AllRoleIdToSkillList.newBuilder();
                aritkl.setAllRoleId(allRoleId);
                skillVOMap.values().forEach(skillVO -> {
                    GameEntity.Skill.Builder sb = GameEntity.Skill.newBuilder();
                    sb.setId(skillVO.getId());
//                    sb.setBuffAddATN(skillVO.getNumberMap().get(CommonBase.BUFF_ADD_ATN));
//                    sb.setBuffATN(skillVO.getNumberMap().get(CommonBase.BUFF_ATN));
                    sb.setCostHP(skillVO.getCostHP());
                    sb.setCD(skillVO.getCD());
                    sb.setPriority(skillVO.getPriority());
                    sb.setName(skillVO.getName());
                    sb.setLabel(skillVO.getLabel());
                    sb.setDuration(skillVO.getDuration());
                    sb.setReading(skillVO.getReading());
                    aritkl.addSkill(sb.build());
                });
                ssr.addAllRoleIdToSkillList(aritkl.build());
            }
        });

        srbb.addSGameSkillResponse(ssr.build());
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Gate,
                "", PtoNum.SKILLRESPONSEBROADCAST, srbb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }


    /**
     * 获取技能信息返回
     *
     * @param code
     * @param content
     * @param userId
     * @param netId
     * @param allRoleIdToSkillVOMap
     */
    public void sendGameSkillResponse(int code, String content, String userId, long netId, Map<String, Map<String, SkillVO>> allRoleIdToSkillVOMap) {
        Game.SGameSkillResponse.Builder ssr = Game.SGameSkillResponse.newBuilder();
        ssr.setCode(code);
        ssr.setContent(content);
        allRoleIdToSkillVOMap.forEach((allRoleId, skillVOMap) -> {
            if (skillVOMap != null) {
                Game.AllRoleIdToSkillList.Builder aritkl = Game.AllRoleIdToSkillList.newBuilder();
                aritkl.setAllRoleId(allRoleId);
                skillVOMap.values().forEach(skillVO -> {
                    GameEntity.Skill.Builder sb = GameEntity.Skill.newBuilder();
                    sb.setId(skillVO.getId());
//                    sb.setBuffAddATN(skillVO.getNumberMap().get(CommonBase.BUFF_ADD_ATN));
//                    sb.setBuffATN(skillVO.getNumberMap().get(CommonBase.BUFF_ATN));
                    sb.setCostHP(skillVO.getCostHP());
                    sb.setCD(skillVO.getCD());
                    sb.setPriority(skillVO.getPriority());
                    sb.setName(skillVO.getName());
                    sb.setLabel(skillVO.getLabel());
                    sb.setDuration(skillVO.getDuration());
                    sb.setReading(skillVO.getReading());
                    aritkl.addSkill(sb.build());
                });
                ssr.addAllRoleIdToSkillList(aritkl.build());
            }
        });

        Game.SGameSkillResponse sGameSkillResponse = ssr.build();
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                netId, userId, PtoNum.SGAMESKILLRESPONSE, sGameSkillResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendGameAttackResponse(int code, String content, String userId, String allRoleId, long netId, String skillId) {
        Game.SGameAttackResponse.Builder sarb = Game.SGameAttackResponse.newBuilder();
        sarb.setCode(code);
        sarb.setContent(content);
        sarb.setSenderId(allRoleId);
        sarb.setSkillId(skillId);
        Game.SGameAttackResponse sGameAttackResponse = sarb.build();
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                netId, userId, PtoNum.SGAMEATTACKRESPONSE, sGameAttackResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendGameAttackResponse(int code, String content, String userId, String allRoleId, long netId, String skillId, List<AllRoleVO> allRoleVOList) {
        Game.SGameAttackResponse.Builder sarb = Game.SGameAttackResponse.newBuilder();
        sarb.setCode(code);
        sarb.setContent(content);
        sarb.setSenderId(allRoleId);
        sarb.setSkillId(skillId);
        allRoleVOList.forEach(allRoleVO -> {
            GameEntity.AllRole.Builder arb = GameEntity.AllRole.newBuilder();
            allRoleVOToAllRole(arb, allRoleVO);
            sarb.addAllRole(arb.build());
        });
        Game.SGameAttackResponse sGameAttackResponse = sarb.build();
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                netId, userId, PtoNum.SGAMEATTACKRESPONSE, sGameAttackResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 广播更新实体消息
     *
     * @param code
     * @param allUserIdSet 接受对象
     * @param allRoleVO    需要更新的实体
     */
    public void sendAoiBroadcastResponse(int code, Set<String> allUserIdSet, AllRoleVO allRoleVO) {
        GameServerResponse.AoiBroadcastResponse.Builder abrb = GameServerResponse.AoiBroadcastResponse.newBuilder();
        abrb.setCode(code);
        GameEntity.stringMsg.Builder smb = GameEntity.stringMsg.newBuilder();
        for (String s : allUserIdSet) {
            smb.setStringId(s);
            abrb.addReceiverId(smb.build());
        }
        GameEntity.AllRole.Builder arb = GameEntity.AllRole.newBuilder();
        allRoleVOToAllRole(arb, allRoleVO);
        abrb.addAllRole(arb.build());
        GameServerResponse.AoiBroadcastResponse aoiBroadcastResponse = abrb.build();
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                1, "", PtoNum.AOIBROADCASTRESPONSE, aoiBroadcastResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 更新角色信息
     *
     * @param code
     * @param allUserIdSet
     * @param roleVOList
     */
    public void sendAoiRoleBroadcastResponse(int code, Set<String> allUserIdSet, List<RoleVO> roleVOList) {
        GameServerResponse.AoiRoleBroadcastResponse.Builder arbrb = GameServerResponse.AoiRoleBroadcastResponse.newBuilder();
        arbrb.setCode(code);
        GameEntity.stringMsg.Builder smb = GameEntity.stringMsg.newBuilder();
        for (String s : allUserIdSet) {
            smb.setStringId(s);
            arbrb.addReceiverId(smb.build());
        }
        GameEntity.Role.Builder rb = GameEntity.Role.newBuilder();
        for (RoleVO roleVO : roleVOList) {
            roleVOToRole(rb, roleVO);
            arbrb.addRole(rb.build());
        }
        GameServerResponse.AoiRoleBroadcastResponse aoiRoleBroadcastResponse = arbrb.build();
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                1, "", PtoNum.AOIROLEBROADCASTRESPONSE, aoiRoleBroadcastResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 装备请求的返回方法
     *
     * @param code
     * @param content
     * @param weaponryVOList
     * @param messageVO
     */
    public void sendWeaponryResponse(int code, String userId, String content, List<WeaponryVO> weaponryVOList, MessageVO messageVO) {
        GameBackpack.SWeaponryResponse.Builder swrb = GameBackpack.SWeaponryResponse.newBuilder();
        for (WeaponryVO weaponryVO : weaponryVOList) {
            GameEntity.Weaponry.Builder wb = GameEntity.Weaponry.newBuilder();
            weaponryVOToWeaponry(wb, weaponryVO);
            swrb.addWeaponry(wb.build());
        }
        swrb.setCode(code);
        swrb.setContent(content);
        swrb.setUserId(userId);
        GameBackpack.SWeaponryResponse sWeaponryResponse = swrb.build();
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                messageVO.getNetId(), messageVO.getUserId(), PtoNum.SWEAPONRYRESPONSE, sWeaponryResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 装备请求的返回方法
     * @param code
     * @param content
     * @param weaponryVOMap
     * @param messageVO
     */
    public void sendWeaponryResponse(int code, String content, Map<String, WeaponryVO> weaponryVOMap, MessageVO messageVO) {
        GameBackpack.SWeaponryResponse.Builder swrb = GameBackpack.SWeaponryResponse.newBuilder();
        if (weaponryVOMap != null){
            for (WeaponryVO weaponryVO : weaponryVOMap.values()) {
                GameEntity.Weaponry.Builder wb = GameEntity.Weaponry.newBuilder();
                weaponryVOToWeaponry(wb, weaponryVO);
                swrb.addWeaponry(wb.build());
            }
        }
        swrb.setCode(code);
        swrb.setContent(content);
        swrb.setUserId(messageVO.getUserId());
        GameBackpack.SWeaponryResponse sWeaponryResponse = swrb.build();
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                messageVO.getNetId(), messageVO.getUserId(), PtoNum.SWEAPONRYRESPONSE, sWeaponryResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 物品的请求应答
     *
     * @param code
     * @param content
     * @param itemVOMap
     * @param messageVO
     */
    public void sendItemResponse(int code, String content, Map<String, ItemVO> itemVOMap, MessageVO messageVO) {
        GameBackpack.SItemResponse.Builder sirb = GameBackpack.SItemResponse.newBuilder();
        if (itemVOMap != null) {
            itemVOMap.values().forEach(itemVO -> {
                GameEntity.Backpack.Builder bb = GameEntity.Backpack.newBuilder();
                GameEntity.Item.Builder ib = GameEntity.Item.newBuilder();
                bb.setBackpackId(itemVO.getBackpackId());
                bb.setName(itemVO.getName());
                bb.setMaxRepetition(itemVO.getMaxRepetition());
                bb.setLabel(itemVO.getLabel());
                ib.setCount(itemVO.getCount());
                ib.setBackpack(bb.build());
                sirb.addItem(ib.build());
            });
        }
        sirb.setCode(code);
        sirb.setContent(content);
        GameBackpack.SItemResponse sItemResponse = sirb.build();
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                messageVO.getNetId(), messageVO.getUserId(), PtoNum.SITEMRESPONSE, sItemResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 商品消息返回
     *
     * @param code
     * @param content
     * @param commodityConfigVOList
     */
    public void sendShopResponse(int code, String content, List<CommodityConfigVO> commodityConfigVOList, String userId) {
        GameShop.SShopResponse.Builder sShopResponse = GameShop.SShopResponse.newBuilder();
        sShopResponse.setCode(code);
        sShopResponse.setContent(content);
        if (commodityConfigVOList != null) {
            commodityConfigVOList.forEach(commodityVO -> {
                GameEntity.Commodity.Builder cmb = GameEntity.Commodity.newBuilder();
                cmb.setId(commodityVO.getCommodityId());
                cmb.setLevelLimit(commodityVO.getLevelLimit());
                cmb.setMoney(commodityVO.getMoney());
                cmb.setMoneyType(commodityVO.getMoneyType());
                cmb.setName(commodityVO.getName());
                sShopResponse.addCommodity(cmb.build());
            });
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.SSHOPRESPONSE, sShopResponse.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);

    }

    /**
     * 更新用户情况
     * @param userVO
     */
    public void sendUserVO(UserVO userVO){
        GameEntity.User.Builder ub = GameEntity.User.newBuilder();
        ub.setUserId(userVO.getUserId());
        ub.setName(userVO.getName());
        ub.setIngot(userVO.getIngot());
        ub.setGold(userVO.getGold());
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userVO.getUserId(), PtoNum.USER, ub.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 发送装备广播协议
     *
     * @param code
     * @param userIdToWeaponryMap
     * @param allUserIdSet
     */
    public void sendWeaponryBroadcastResponse(int code, Map<String, List<WeaponryVO>> userIdToWeaponryMap, Set<String> allUserIdSet, String roleId) {
        GameBackpack.SWeaponryBroadcastResponse.Builder swbrb = GameBackpack.SWeaponryBroadcastResponse.newBuilder();
        for (String userId : allUserIdSet) {
            swbrb.addReceiverId(GameEntity.stringMsg.newBuilder().setStringId(userId).build());
        }
        GameBackpack.UserIdToWeaponry.Builder uitwb = GameBackpack.UserIdToWeaponry.newBuilder();
        userIdToWeaponryMap.forEach((s, weaponryVOList) -> {
            uitwb.setCode(code);
            uitwb.setUserId(s);
            for (WeaponryVO weaponryVO : weaponryVOList) {
                GameEntity.Weaponry.Builder wb = GameEntity.Weaponry.newBuilder();
                weaponryVOToWeaponry(wb, weaponryVO);
                uitwb.setRoleId(roleId);
                uitwb.addWeaponry(wb.build());
            }
            swbrb.addUserIdToWeaponry(uitwb.build());
        });
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Gate,
                "", PtoNum.SWEAPONRYBROADCASTRESPONSE, swbrb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 物品使用情况广播
     *
     * @param code
     * @param userIdSet
     * @param roleId
     * @param userId
     * @param itemId
     */
    public void sendItemBroadcastResponse(int code, Set<String> userIdSet, String roleId, String userId, String itemId) {
        GameBackpack.UseItemBroadcastResponse.Builder uibrb = GameBackpack.UseItemBroadcastResponse.newBuilder();
        for (String userId1 : userIdSet) {
            uibrb.addReceiverId(GameEntity.stringMsg.newBuilder().setStringId(userId1).build());
        }
        GameBackpack.UserIdToItem.Builder uitib = GameBackpack.UserIdToItem.newBuilder();
        uitib.setCode(code);
        uitib.setItemId(itemId);
        uitib.setUserId(userId);
        uitib.addRoleId(GameEntity.stringMsg.newBuilder().setStringId(roleId).build());
        uibrb.addUserIdToItem(uitib.build());
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Gate,
                "", PtoNum.USEITEMBROADCASTRESPONSE, uibrb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 发送用户对应角色消息
     *
     * @param userIdToRoleVoMap
     * @param userId
     */
    public void sendRoleResponse(Map<String, List<String>> userIdToRoleVoMap, String userId) {
        Game.SGameRoleResponse.Builder srrb = Game.SGameRoleResponse.newBuilder();
        GameEntity.Role.Builder rb = GameEntity.Role.newBuilder();
        GameEntity.AllRole.Builder arb = GameEntity.AllRole.newBuilder();
        userIdToRoleVoMap.forEach((userId1, roleIdList) -> {
            Game.UserIdToRole.Builder uitr = Game.UserIdToRole.newBuilder();
            uitr.setUserId(userId1);
            for (String roleId : roleIdList) {
                RoleVO roleVO = roleServiceCache.getRoleServiceMap().get(roleId).getRoleVO();
                arb.setAllRoleId(roleVO.getAllRoleId());
                arb.setLabel(roleVO.getLabel());
                arb.setStatus(roleVO.getStatus());
                arb.setINT(roleVO.getNumberMap().get(CommonBase.INT));
                arb.setATN(roleVO.getNumberMap().get(CommonBase.ATN));
                arb.setDEF(roleVO.getNumberMap().get(CommonBase.DEF));
                arb.setRES(roleVO.getNumberMap().get("RES"));
                arb.setNickname(roleVO.getName());
                arb.setHP(roleVO.getNumberMap().get(CommonBase.HP));
                arb.setMP(roleVO.getNumberMap().get(CommonBase.MP));
                rb.setAllRole(arb.build());
                rb.setLevel(roleVO.getLevel());
                rb.setUserId(userId1);
                uitr.addRole(rb.build());
            }
            srrb.addUserIdToRole(uitr.build());
        });
        srrb.setCode(Common.GAME_ROLE_SUCCESS);
        Game.SGameRoleResponse sGameRoleResponse = srrb.build();
        ByteBuf setBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.SGAMEROLERESPONSE, sGameRoleResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(setBuf);
    }

    /**
     * 通用AllRoleVO对象转换为Protobuf消息体对象AllRole
     *
     * @param ab
     * @param allRoleVO
     */
    public void allRoleVOToAllRole(GameEntity.AllRole.Builder ab, AllRoleVO allRoleVO) {
        if (!(allRoleVO instanceof NpcVO)) {
            ab.setHP(allRoleVO.getNumberMap().get(CommonBase.HP));
            ab.setMP(allRoleVO.getNumberMap().get(CommonBase.MP));
            ab.setATN(allRoleVO.getNumberMap().get(CommonBase.ATN));
            ab.setDEF(allRoleVO.getNumberMap().get(CommonBase.DEF));
            ab.setINT(allRoleVO.getNumberMap().get(CommonBase.INT));
            ab.setRES(allRoleVO.getNumberMap().get(CommonBase.RES));
        }
        ab.setStatus(allRoleVO.getStatus());
        ab.setAllRoleId(allRoleVO.getAllRoleId());
        ab.setNickname(allRoleVO.getName());
        ab.setLabel(allRoleVO.getLabel());
        if (allRoleVO.getAttackEntityId() != null) {
            for (String attackEntityId : allRoleVO.getAttackEntityId()) {
                ab.addAttackEntityId(GameEntity.stringMsg.newBuilder().setStringId(attackEntityId).build());
            }
        }
    }

    /**
     * 角色RoleVO转换为Protobuf消息体对象Role
     *
     * @param rb
     * @param roleVO
     */
    public void roleVOToRole(GameEntity.Role.Builder rb, RoleVO roleVO) {
        GameEntity.AllRole.Builder ab = GameEntity.AllRole.newBuilder();
        ab.setHP(roleVO.getNumberMap().get(CommonBase.HP));
        ab.setMP(roleVO.getNumberMap().get(CommonBase.MP));
        ab.setATN(roleVO.getNumberMap().get(CommonBase.ATN));
        ab.setDEF(roleVO.getNumberMap().get(CommonBase.DEF));
        ab.setINT(roleVO.getNumberMap().get(CommonBase.INT));
        ab.setRES(roleVO.getNumberMap().get(CommonBase.RES));
        ab.setStatus(roleVO.getStatus());
        ab.setAllRoleId(roleVO.getAllRoleId());
        ab.setNickname(roleVO.getName());
        ab.setLabel(roleVO.getLabel());
        if (roleVO.getAttackEntityId() != null) {
            for (String attackEntityId : roleVO.getAttackEntityId()) {
                ab.addAttackEntityId(GameEntity.stringMsg.newBuilder().setStringId(attackEntityId).build());
            }
        }
        rb.setAllRole(ab.build());
        rb.setLevel(roleVO.getLevel());
        rb.setUserId(roleVO.getUserId());

    }

    public void weaponryVOToWeaponry(GameEntity.Weaponry.Builder wb, WeaponryVO weaponryVO) {
        GameEntity.Backpack.Builder bb = GameEntity.Backpack.newBuilder();
        bb.setBackpackId(weaponryVO.getBackpackId());
        bb.setLabel(weaponryVO.getLabel());
        bb.setName(weaponryVO.getName());
        bb.setMaxRepetition(weaponryVO.getMaxRepetition());
        bb.setBuffAddANT(weaponryVO.getNumberMap().get(CommonBase.BUFF_ADD_ATN));
        bb.setBuffAddINT(weaponryVO.getNumberMap().get(CommonBase.BUFF_ADD_INT));
        bb.setBuffAddDEF(weaponryVO.getNumberMap().get(CommonBase.BUFF_ADD_DEF));
        bb.setBuffAddRES(weaponryVO.getNumberMap().get(CommonBase.BUFF_ADD_RES));
        wb.setBackpack(bb.build());
        wb.setSelfWeaponryId(weaponryVO.getSelfWeaponryId());
        wb.setDurability(weaponryVO.getDurability());
        wb.setLevel(weaponryVO.getLevel());
        wb.setUsage(weaponryVO.getUsage());
        wb.setPart(weaponryVO.getPart());
        wb.setProfession(weaponryVO.getProfession());
    }
}
